package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.EduTeacher;
import com.atguigu.edu.mapper.EduTeacherMapper;
import com.atguigu.edu.service.EduTeacherService;
import com.atguigu.request.EduTeacherConditionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.classfile.Unknown;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author HB
 * @since 2020-11-01
 * ServiceImpl里面注入了
 *   @Autowired
 *     protected M baseMapper;
 *     意思就是以前的service调用dao 他帮我们做了
 */
@Service

public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    /**
     * 通过条件进行分页查询
     * @param teacherPage
     * @param eduTeacherConditionVO
     */
    @Override
    public void queryTeacherPageByCondition(Page<EduTeacher> teacherPage, EduTeacherConditionVO eduTeacherConditionVO) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = eduTeacherConditionVO.getName();
        Integer level = eduTeacherConditionVO.getLevel();
        String beginTime = eduTeacherConditionVO.getBeginTime();
        String endTime = eduTeacherConditionVO.getEndTime();
        /**
         * 判断讲师名字是否为空
         * if (!StringUtils.isEmpty(name))名字不为空
         */
        if (!StringUtils.isEmpty(name)) {
            //对名字进行模糊查询
            queryWrapper.like("name",name);
        }
        //时间不为空进去进行逻辑判断
        if (!StringUtils.isEmpty(beginTime)) {

            //java.sql.SQLSyntaxErrorException: Unknown column 'beginTime' in 'where clause'
            //当时手抖写成了下面的导致报错，错误代码已注释，正确代码如下
//            queryWrapper.ge("beginTime",beginTime);
            queryWrapper.ge("gmt_create",beginTime);
        }
        //时间不为空进去进行逻辑判断
        if (!StringUtils.isEmpty(endTime)) {

//            java.sql.SQLSyntaxErrorException: Unknown column 'beginTime' in 'where clause'
//            queryWrapper.le("endTime",endTime);
            queryWrapper.ge("gmt_create",endTime);
        }
        //等级不为空
        if (level != null) {
            queryWrapper.eq("level",level);
        }
        queryWrapper.orderByDesc("gmt_create");

        /**
         *      @param page         分页查询条件（可以为 RowBounds.DEFAULT）
         *      @param queryWrapper 实体对象封装操作类（可以为 null）
         */
        baseMapper.selectPage(teacherPage,queryWrapper);
    }
}
